﻿using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// 日历字段
    /// </summary>
    public class DateField : ElementField
    {
        /// <summary>
        /// 日历字段
        /// </summary>
        /// <param name="fieldName"></param>
        public DateField(string fieldName)
        {
            this.FieldName = fieldName;
        }
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return $"@Html.Bootstrap().LableFor(m => m.{this.FieldName})" + base.Build(nonFirst);
            }
            var sb = new StringBuilder();
            sb.AppendFormat("<div class=\"input-group date\" id=\"div_{0}\">", FieldName);
            sb.AppendFormat("@Html.Bootstrap().TextBoxFor(m => m.{0})", FieldName);
            sb.Append(".AddClass(\"pull-left\")");
            sb.Append("<div class=\"input-group-addon\"><i class=\"fa fa-calendar\"></i></div></div>");

            sb.AppendFormat("@Html.ValidationMessageFor(m => m.{0})", FieldName);
            sb.AppendFormat("<script type = \"text/javascript\">\r\n$('#div_{0}').datepicker({{ autoclose: true,format: 'yyyy-mm-dd',language: 'zh-CN' }}).on(\"changeDate\", function(e) {{ $(e.target).find('input').valid(); }});\r\n</script>", FieldName);

            return sb.ToString();
        }
    }
}
